//
//  AppDelegate.m
//  webDemo
//
//  Created by 何涛 on 2017/6/27.
//  Copyright © 2017年 thomasho. All rights reserved.
//

#import "AppDelegate.h"
#import "WebVC.h"
#import "BRTIDFA.h"

#define urls @[@{@"Certis Office":@"http://p.brtbeacon.net/custom/singapore/en_navigator.html?signa=f27d1ab82b004a7cb7b5feb69e00633d"},\
            @{@"KTPH":@"http://p.brtbeacon.net/custom/singapore/en_navigator.html?signa=ace3ebfd7ca7449eae071e76ea6a2349"},\
            @{@"KKH":@"http://p.brtbeacon.net/custom/singapore/en_navigator.html?signa=9f1a37dbd6b844ad8330f467de889c65"}]

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    NSString *urlStr = [NSString stringWithFormat:@"%@&mac=%@",urls[0][@"Certis Office"],[BRTIDFA value]];
    WebVC *webVC = [[WebVC alloc] initWithUrl:urlStr];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:webVC];
    self.window.rootViewController = nav;
    
    nav.topViewController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"切换", nil) style:UIBarButtonItemStylePlain target:self action:@selector(switchButtonClicked:)];
    
//    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(customUrl)];
//    nav.navigationItem.rightBarButtonItem = item;
    
    [self.window makeKeyAndVisible];
    return YES;
}

- (IBAction)switchButtonClicked:(id)sender {
    UINavigationController *nav = (UINavigationController *)self.window.rootViewController;
    WebVC *webVC = nav.viewControllers.firstObject;
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"切换", nil) message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    for (NSDictionary *item in urls) {
        NSString *urlStr = [NSString stringWithFormat:@"%@&mac=%@",item.allValues[0],[BRTIDFA value]];
        UIAlertAction *action = [UIAlertAction actionWithTitle:item.allKeys[0] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [webVC refresh:urlStr];
        }];
        [alert addAction:action];
    }
    if (alert.popoverPresentationController) {
        alert.popoverPresentationController.sourceView = webVC.view;
        alert.popoverPresentationController.sourceRect = CGRectMake(0, webVC.view.bounds.size.height, webVC.view.bounds.size.width, 1);
        alert.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionDown;
    }
    [self.window.rootViewController presentViewController:alert animated:YES completion:nil];
}

//- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
//{
//    UINavigationController *nav = (UINavigationController *)self.window.rootViewController;
//    WebVC *webVC = nav.viewControllers.firstObject;
//    NSString *urlStr = [url.absoluteString stringByReplacingOccurrencesOfString:url.scheme withString:@"http"];
//    if (!webVC) {
//        webVC = [[WebVC alloc] initWithUrl:urlStr];
//        self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:webVC];
//    }else{
//        [webVC refresh:urlStr];
//    }
//    return YES;
//}
//
//-(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
//{
//    return YES;
//}

@end
